package com.chance.cc.crawler.prod.command.job.domain.commerce.tb;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.scripts.taobao.TaobaoH5ProductDetailCrawlerScript;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerDataType.article;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * created by CC on 2020/11/26
 * mail 279020185@qq.com
 */
public class TaobaoH5ProductDetailCrawlerScheduleJob extends TBCommonCrawlerSchedulerJob {

    public static void main(String[] args) {
        publishTBDetailSchedulerJob();
    }

    public static CrawlerJob publishTBDetailSchedulerJob(){
        CrawlerJob taobaoSchduler = taobaoSchduler();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(taobaoSchduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("淘宝商品明细定时采集");
        crawlerScheduleJob.setCrawlerKey(taobaoSchduler.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());

        return taobaoSchduler;
    }

    /**
     * taobao采集
     * @return
     */
    public static CrawlerJob taobaoSchduler() {

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl(TaobaoH5ProductDetailCrawlerScript.startUrl)
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .resultLabelTag(article)
                .proxy(proxy)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.getHttpConfig().setDisableCookie(true);
        requestRecord.tagsCreator().bizTags().addDomain(TaobaoH5ProductDetailCrawlerScript.domain);
        requestRecord.tagsCreator().bizTags().addSite(TaobaoH5ProductDetailCrawlerScript.site);

        String triggerJobKey = StringUtils.joinWith("-", TaobaoH5ProductDetailCrawlerScript.site,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime);
        String triggerKey = CrawlerMetaConstant.ScheduleJobTrigger_Cron;

        //初始 comment crawler request record
        String requestQueueName = StringUtils.joinWith("-","crawler",TaobaoH5ProductDetailCrawlerScript.domain,
                TaobaoH5ProductDetailCrawlerScript.site,"detail","queue");

        CrawlerRequestRecord initCommentCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("taobao_keywords",turnPageItem)
                .httpUrl(metaServiceHttpPrefix + "/crawler/domain/common/api/v1/taobao/results/queue/init?requestQueueName="+requestQueueName)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        HttpRequestBody jsonBody = HttpRequestBody.json(JSON.toJSONString(requestRecord), "utf-8");
        initCommentCrawlerRecord.getHttpRequest().setMethod(HttpConstant.Method.POST);
        initCommentCrawlerRecord.getHttpRequest().setRequestBody(jsonBody);

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(5)
                .triggerInfo(
                        domain,
                        triggerKey,
                        System.currentTimeMillis(),
                        triggerJobKey)
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .fileResultPipeline(null, "/data/chance_crawler_runner/data/taobao/tb_h5_product.json", false)
                .kafkaResultPipeline(null, kafkaTopic, null)
                .requestRecord(requestRecord)
                .supportRecord(initCommentCrawlerRecord)
                .build();
//        crawlerJob.setInitRequestQueue(false);

        return crawlerJob;
    }

}
